

#import "INCell.h"

#import "FMCell.h"
#import "Masonry.h"

#import "CellHeadView.h"
#import "SectionHeader.h"
#import "CellFootView.h"
#import "SpecialCell.h"


#define  CELL_HEIGHT 40


@interface INCell()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableview;

@property(nonatomic,strong)CellFootView *footView;//tableFooterView



@end

static NSString *FMCellIdenti = @"FMCell";
static NSString *SpecialCellIdenti = @"SpecialCell";


@implementation INCell

-(CellFootView *)footView{
    if (!_footView) {
        _footView = [[NSBundle mainBundle] loadNibNamed:@"CellFootView" owner:nil options:nil].lastObject;
        _footView.frame = CGRectMake(0, 0, self.bounds.size.width, CELL_HEIGHT);
    }
    return  _footView;
}

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        
        _tableview.delegate = self;
        _tableview.dataSource = self;
        
        _tableview.scrollEnabled = NO;
        
        _tableview.backgroundColor = [UIColor greenColor];
        
        _tableview.estimatedRowHeight = CELL_HEIGHT;
        _tableview.rowHeight = UITableViewAutomaticDimension;
        
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        
     
        
        
        UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, CELL_HEIGHT)];
        [foot addSubview: self.footView];
        _tableview.tableFooterView = foot;
        
        [_tableview registerNib:[UINib nibWithNibName:FMCellIdenti bundle:nil] forCellReuseIdentifier:FMCellIdenti];
        [_tableview registerNib:[UINib nibWithNibName:SpecialCellIdenti bundle:nil] forCellReuseIdentifier:SpecialCellIdenti];
        

        
        
    }
    return  _tableview;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        [self addSubview:self.tableview];
    }
    return self;
}

//请求回来网络数据后 让tableView 显示
-(void)setModel:(OutModel *)model{
    _model = model;
    
    [self addSubview:self.tableview];
    
    [self setNeedsLayout];
    
    if (model.works.count != 0) {
        self.footView.worksArray = model.works;
    }
   
    
    [self makeConstraintForTableView];
    [self.tableview reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
}



#pragma mark --tableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.model.accessories.count + 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return  1;
    }else{
         return  [[self.model.accessories[section-1] accessory] count];
    }
    
   
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    
    if (indexPath.section == 0) {
        SpecialCell *cellS   = [tableView dequeueReusableCellWithIdentifier:SpecialCellIdenti];
        cellS.model = self.model;
        
        cell = cellS;
    }else{
        FMCell *cellF = [tableView dequeueReusableCellWithIdentifier:FMCellIdenti];
        
        cellF.model = [self.model.accessories[indexPath.section -1] accessory][indexPath.row];
        
        cell = cellF;
    }
    
    
    return  cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  CELL_HEIGHT ;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
   
    if (section == 0) {
        return  nil;
    }
    SectionHeader *header = [[NSBundle mainBundle] loadNibNamed:@"SectionHeader" owner:nil options:nil].lastObject;
    
    header.model = self.model.accessories[section - 1];
    
    return  header;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return  0;
    }
    
    return  self.model.works.count == 0 ? 0.1 : CELL_HEIGHT;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        //删除按钮方法
    }
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  @"删除";
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return  UITableViewCellEditingStyleDelete;
    }else{
        return UITableViewCellEditingStyleNone;
    }
    
    
}





#pragma mark --privateMethod
//给tableview 加约束
-(void)makeConstraintForTableView{
    
    __weak typeof(self) weakSelf = self;
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf);
        make.right.equalTo(weakSelf);
        make.bottom.equalTo(weakSelf);
        make.top.equalTo(weakSelf);
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
